<template>
	<view class="mainbgc">
		<header-top :messageInfo="{typeFlag:true,message:'员工档案',liner:false}">
			<view class="rightmsg" slot="rightmsg">
				<view class="rightmsg_content flexright">
					<view class="add" @click="jumpurl('/pages/workbench/jiben/yuangongdaadd',2)"></view>
				</view>
			</view>
		</header-top>
		<view class="mainpadding">
			<view class="flexleft">
				<view class="flexbetween searchbox margin_right">
					<input type="text" placeholder="输入员工名称" v-model="keyword" confirm-type="search" @confirm="init()">
					<view class="searchimg"></view>
				</view>
				<view class="flexbetween searchbox margin_right" @click="ckPianQuShow=true">
					<input type="text" placeholder="点击选择片区" disabled="true" v-model="ckPianQuText">
					<view class="righticon2"></view>
				</view>
				<view class="flexbetween searchbox margin_right" @click="ygZhiWeiShow=true" v-show="type!=1">
					<input type="text" placeholder="点击选择职位" disabled="true" v-model="ygZhiWeiText">
					<view class="righticon2"></view>
				</view>
			</view>
			<view class="strongtext flexbetween margin_top">
				<view style="min-width: 200rpx;">共计：{{list.total}}个</view>
			</view>
			<view class="flexcenter" v-if="!list.total">
				<view class="placeholderimage"></view>
			</view>
			<view class="mainpadding margin_top ffffff radius" v-for="item in list.data" :key="item.id"
				v-if="list.total" @click="selectItemYG(item)">
				<view style="position: relative;">
					<view class="userinfo flexleft">
						<view class="titletext margin_right">{{item.username}} {{item.mobile}}</view>
						<view class="girl" v-show="item.gender==2"></view>
						<view class="boy" v-show="item.gender==1"></view>
					</view>
					<view class="ylable importfive flexcenter">{{item.staffbm.name}}</view>
				</view>
				<view class="flexbetween">
					<view class="dizhi">
						<view class="dizhi margin_top">
							<text class="margin_right">职位：{{item.position_list_text}}</text>
							<text>部门：{{item.staffbm.name}}</text>
						</view>
						<view class="dizhi margin_top">地址：{{item.address}}</view>
						<view class="dizhi margin_top">入职时间：{{date(item.rz_time*1000)}}</view>
					</view>
					<view class="flexright">
						<!-- <view class="addressicon margin_right" @click.stop="openmap(item)"></view> -->
						<view class="phoneicon" @click.stop="phone(item.mobile)"></view>
					</view>
				</view>
				<view class="flexright margin_top">
					<view class="xiaolankuang flexbetween margin_left2"
						@click.stop="edit('/pages/workbench/jiben/yuangongdaadd?id=',item.id,3)">
						<u-icon name="edit-pen" color="#0BBDCB" size="16"></u-icon>
						<view class="xiaolan margin_left1">编辑</view>
					</view>
					<view class="xiaolankuang flexbetween margin_left2" @click.stop="delItem(item.id,4)">
						<u-icon name="trash" color="#0BBDCB" size="16"></u-icon>
						<view class="xiaolan margin_left1">删除</view>
					</view>
				</view>
			</view>
		</view>
		<selectCKPianQu :message="{ckPianQuShow:ckPianQuShow}" @ckPianQuSure="ckPianQuSure"></selectCKPianQu>
		<!-- 仓库片区 -->
		<selectYGZhiWei :message="{ygZhiWeiShow:ygZhiWeiShow}" @ygZhiWeiSure="ygZhiWeiSure"></selectYGZhiWei>
		<!-- 员工职位 -->
		<moDal :message="{modalShow:modal.modalShow,title:modal.title}" @modalSure="modalSure"></moDal><!-- 模态框 -->
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				// 模态框
				modal: {
					modalShow: false,
					title: "",
				},
				// 员工片区
				ckPianQuShow: false,
				ckPianQuText: "",
				staff_area_id: "", //员工片区id
				// 员工职位
				ygZhiWeiShow: false,
				ygZhiWeiText: "",
				position_list: "", //员工职位id
				page: 1,
				limit: 12,
				list: {
					data: [],
					total: "",
				},
				keyword: "",
				usergroup: 0, //用户信息
				type: "", //1选择送货员
				currentId: "",
			}
		},
		onLoad(options) {
			this.type = options.type
			if (this.type == 1) { //选择送货员
				this.position_list = 2
				this.ygZhiWeiText = "送货员"
			}
			if (this.type == 3) { //选择仓库管理员
				this.position_list = 3
				this.ygZhiWeiText = "仓库管理员"
			}

		},
		onShow() {
			this.init()
		},
		methods: {
			delItem(id,qxpdid) {
				if (!httpRequest.qxpd(qxpdid, this.$store.state.userInfo.ability_rule_ids)) {
					return false
				}
				this.modal.modalShow = true
				this.modal.title = "确定删除当前内容吗"
				this.currentId = id
			},
			modalSure() {
				httpRequest.request('/dhtapi/user/deleteStaff', 'POST', {
					id: this.currentId
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						this.init()
					}
				})
			},
			date(timer) {
				var date = new Date(timer); //当前时间
				var year = date.getFullYear() //返回指定日期的年份
				var month = repair(date.getMonth() + 1); //月
				var day = repair(date.getDate()); //日
				var curTime = year + "-" + month + "-" + day

				function repair(i) {
					if (i >= 0 && i <= 9) {
						return "0" + i;
					} else {
						return i;
					}
				}
				return curTime
			},
			ckBuMenSure(e) { //员工部门
				if (e) {
					this.ckBuMenText = e.value[0].name
					this.staff_bm_id = e.value[0].id
				}
				this.ckBuMenShow = false
				this.init()
			},
			ygZhiWeiSure(e) { //员工职位
				if (e) {
					console.log(e, "串子")
					this.ygZhiWeiText = e.name
					this.position_list = e.id
				}
				this.ygZhiWeiShow = false
				this.init()
			},
			ckPianQuSure(e) { //仓库片区
				if (e) {
					this.ckPianQuText = e.value[0].name
					this.staff_area_id = e.value[0].id
				}
				this.ckPianQuShow = false
				this.init()
			},
			init(isPage, page) {
				let _this = this;
				httpRequest.request('/dhtapi/user/staffList', 'POST', {
					page: page || 1,
					limit: _this.limit,
					keywords: _this.keyword,
					position_list: this.position_list,
					staff_area_id: this.staff_area_id,
				}, false, false, true).then(res => {
					console.log(res, 6363);
					if (res.code == 1) {
						let resList = res.data.data;
						if (isPage == true) {
							_this.list.data = _this.list.data.concat(resList);
							// _this.list.data.push(...resList);
						} else {
							_this.list = res.data
							_this.list.data = resList;
						}
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			onReachBottom() {
				let _this = this;
				let list = _this.list;
				let page = _this.page;
				if (list.last_page <= page) {
					return false;
				}
				page++;
				_this.page = page;
				_this.init(true, page);
			},
			jumpurl(url,qxpdid) {
				if (!httpRequest.qxpd(qxpdid, this.$store.state.userInfo.ability_rule_ids)) {
					return false
				}
				uni.navigateTo({
					url: url
				})
			},
			// 打开地图
			openmap(item) {
				uni.openLocation({
					latitude: Number(item.latitude), //即将传到高德或腾讯地图的终点纬度  必须是数值，字符串无效
					longitude: Number(item.longitude), //即将传到高德或腾讯地图的终点经度  必须是数值，字符串无效
					name: item.address, //即将传到高德或腾讯地图的店铺名称
					address: item.address, //即将传到高德或腾讯地图的详细地址
				})
			},
			phone(p) {
				uni.makePhoneCall({
					phoneNumber: p.toString()
				}).catch((e) => {
					// console.log(e)  //用catch(e)来捕获错误{makePhoneCall:fail cancel}
				})
			},
			edit(url, val,qxpdid) {
				if (!httpRequest.qxpd(qxpdid, this.$store.state.userInfo.ability_rule_ids)) {
					return false
				}
				uni.navigateTo({
					url: url + val
				})
			},
			selectItemYG(item) {
				if (this.type == 1 || this.type == 2) {
					let data = {
						id: item.id,
						name: item.username,
						item
					}
					uni.$emit('ygSelectFn', data)
					uni.navigateBack(1)
					return false
				}else{
					this.edit('/pages/workbench/jiben/yuangongdaadd?id=',item.id,3)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	// 顶部搜索
	.searchimg {
		width: 24rpx;
		height: 24rpx;
		background-image: url("@/static/image/system/search.png");
		background-size: 100% 100%;
	}

	.searchbox {
		width: 35%;
		padding: 12rpx 20rpx;
		box-sizing: border-box;
		background-color: #f3f3f3;
		border-radius: 50rpx;

		input {
			width: 80%;
			font-size: 24rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
		}
	}

	// 用户信息
	.userimg {
		width: 84rpx;
		height: 84rpx;
		margin-right: 30rpx;
		position: relative;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.userinfo {
		width: 80%;
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #666;
	}

	.flex_end {
		display: flex;
		justify-content: space-between;
		align-items: flex-end;
	}

	.addresstext {
		font-size: 26rpx;
		line-height: 30rpx;
		color: #666;
	}
</style>